import Vue from 'vue'
import Vuex from 'vuex'
import { Notification } from "element-ui";
Vue.use(Vuex)
import persistedState from 'vuex-persistedstate'


export default new Vuex.Store({
  state: {
    activeName: 0,//分类页面的切换id
    loginDialog: false,//登录弹出框开关
    userLoginMsg: {},//用户登录的真实信息
    user_id: '',//用户信息id
    shoppingCartData: [],//购物车数据
    orderData: [],
    checkData: [],//被选中的商品

  },
  mutations: {
    //点击登录
    onsubmit(state, obj) {
      state.userLoginMsg = obj
      state.loginDialog = false
    },
    //退出登录状态
    layout(state) {
      state.userLoginMsg = {}
      Notification.error({ title: "成功", message: "退出成功" })
    },
    shoppingCartDatas(state, data) {
      // 设置购物车状态
      state.shoppingCartData = data;
    },
    //详情页面点击登录事件===>添加到购物车
    unshiftAddShop(state, data) {
      state.shoppingCartData.unshift(data)
      console.log(state.shoppingCartData);
    },
    //如果该商品已经被添加一次了就在原有基础上数量加一
    unshifNumberAdd(state, productID) {
      state.shoppingCartData.forEach(item => {
        // console.log(item);
        if (item.productID == productID) {
          if (item.num < item.maxNum) {
            item.num++
          }
        }
      })
    },
    // 根据购物车id删除购物车商品
    deleteShoppingCarts(state, id) {
      for (let i = 0; i < state.shoppingCartData.length; i++) {

        var item = state.shoppingCartData[i];
        console.log(item);

        if (item.id == id) {
          console.log(i);
          state.shoppingCartData.splice(i, 1);
          console.log(state.shoppingCartData);
        }
      }
    },
    checkAll(state, data) {
      // 点击全选按钮，更改每个商品的勾选状态
      for (let i = 0; i < state.shoppingCartData.length; i++) {
        state.shoppingCartData[i].check = data;
      }
    },
    updateShoppingCart(state, payload) {


      state.checkData = payload
      console.log(state.checkData);
      payload.forEach(item => {
        item.check = true
        if (item.check) {
          payload = []

        }
        console.log(item.check);

      })


    },
  },
  actions: {
    shoppingCartDatas({ commit }, data) {
      // 设置购物车状态
      commit('shoppingCartDatas', data)
    },
    //详情页面点击登录事件===>添加到购物车
    unshiftAddShop({ commit }, data) {
      commit("unshiftAddShop", data)
    },
    //如果该商品已经被添加一次了就在原有基础上数量加一
    unshifNumberAdd({ commit }, productID) {
      commit("unshifNumberAdd", productID)
    },
    // 根据购物车id删除购物车商品
    deleteShoppingCarts({ commit }, id) {
      commit("deleteShoppingCarts", id)
      console.log(id);

    },
    updateShoppingCart({ commit }, payload) {
      commit('updateShoppingCart', payload);
    },
    checkAll({ commit }, data) {
      commit('checkAll', data);
    }
  },
  getters: {
    shoppingCartData(state) {
      // 获取购物车状态
      return state.shoppingCartData;
    },
    getNum(state) {
      // 购物车商品总数量
      let totalNum = 0;
      for (let i = 0; i < state.shoppingCartData.length; i++) {
        const number = state.shoppingCartData[i];
        totalNum += number.num;
      }
      return totalNum;
    },
    getCheckGoods(state) {
      // 获取勾选的商品信息
      // 用于确认订单页面
      let checkGoods = [];
      for (let i = 0; i < state.checkData.length; i++) {
        const temp = state.checkData[i];
        if (temp.check) {
          checkGoods.push(temp);
        }
      }
      return checkGoods;
    },
    getCheckNum(state) {
      // 获取购物车勾选的商品数量
      let totalNum = 0;
      for (let i = 0; i < state.checkData.length; i++) {
        const value = state.checkData[i];
        // console.log(value);

        if (value.check) {
          totalNum += value.num;
        }
      }
      return totalNum;
    },
    getTotalPrice(state) {
      // 购物车勾选的商品总价格
      let totalPrice = 0;
      for (let i = 0; i < state.checkData.length; i++) {
        const numberAll = state.checkData[i];
        if (numberAll.check) {
          totalPrice += numberAll.price * numberAll.num;
        }
      }
      return totalPrice;
    }
  },
  modules: {

  },
  plugins: [persistedState()]
})
